﻿var icon_new = Global.contextPath + '/Images/icon_new.gif';
var icon_edit = Global.contextPath + '/Images/ico_edit.gif';
var icon_delete = Global.contextPath + '/Images/ico_del.gif';
var path = Global.contextPath;

var treeInstance = null;
$(function () {
    //树初始化
    treeInstance = $('#treeDiv').tree({//tree为容器ID
        asyn: true, //异步
        rootId: 'root',
        rootText: '系统功能',
        CommandName: 'java.tree.command',
        onNodeClick: leftClick,
        onLoaded: doAfterLoaded,
        params: {
            sqlId: 'privilege.getPrivilegeTree',
            parentId: 'root'
        }, contextMenu: function (_record) {
            //record为树查询获取的行对象，可以根据行对象返回对应的右键菜单项
            var items = [];

            var _id = _record.id;
            var _name = encodeURI(_record.text)
            var parent_id = _record.parentId;
            var parent_name = encodeURI(_record.parentName);

            var systemId = _record.systemId;
            if (_record.id == 'root') {
                items = [{ text: '添加模块', action: addModule, id: _id, icon: icon_new, parent_id: _id, parent_name: _name },
                ];
            } else if (_record.type == 'Module') {//应用系统
                items = [
						{ text: '添加子模块', action: addModule, id: _id, icon: icon_new, parent_id: _id, parent_name: _name },
						{ text: '修改模块', action: updateModule, id: _id, icon: icon_edit, parent_id: parent_id, parent_name:parent_name },
						{ text: '删除模块', action: deleteModule, id: _id, icon: icon_delete, parent_id: parent_id },
						{ text: '添加功能', action: addFunction, id: _id, icon: icon_new, parent_id: _id, parent_name: _name }
					];
            } else if (_record.type == 'Page') {
                items = [
						{ text: '修改功能', action: updateFunction, id: _id, icon: icon_edit, parent_id: parent_id, parent_name: parent_name },
						{ text: '删除功能', action: deleteFunction, id: _id, icon: icon_delete, parent_id: parent_id },
                        { text: '添加操作', action: addOperation, id: _id, icon: icon_new, parent_id: _id, parent_name: parent_name }
					];
            }
            else if (_record.type == 'Element') {
                items = [
						{ text: '修改操作', action: updateOperation, id: _id, icon: icon_edit, parent_id: parent_id, parent_name: parent_name },
						{ text: '删除操作', action: deleteOperation, id: _id, icon: icon_delete, parent_id: parent_id }
					];
            }
            return {
                width: 120,
                items: items
            };
        }
    });
    $('#treeDiv').tree().expandLevel(null, 3);
});

/*
* 左击事件
* 节点事件的参数依次是：（节点的ID，节点名称，当前节点对象，节点对应的HTML代码）
* '当前节点对象'的属性由SQL语句的选择结果决定，
* 可以以这种方式去访问：itemObj.TYPE，注意属性TYPE必须是大写的
*/
function leftClick(id, name, record, currentNode) {
    if (id == 'root') {//ID不存在，点击根节点
        loadModuleContent(id,record);
    } else {
        var type = record.type;
        if (type == 'Page') {
            loadOperatorContent(id, record);
        } else if (type == 'Module') {
            loadFunctionContent(id, record);
        } else if (type == 'Element') {
            loadOperatorRuleContent(id, record);
        }
    }
}

function doAfterLoaded() {
    if (document.getElementById("content_frame").src == "") {
        document.getElementById("content_frame").src = Global.contextPath + '/Modules/SecurityConsole/Privilege/ModuleList.aspx';
    }
}

//右侧内容区加载功能页面
function loadModuleContent(id, record) {
    document.getElementById("content_frame").src = Global.contextPath + '/Modules/SecurityConsole/Privilege/ModuleList.aspx?ParentId=' + id + '&ParentName=' + encodeURI(record.text);
}

function loadFunctionContent(id, record) {
    document.getElementById("content_frame").src = Global.contextPath + '/Modules/SecurityConsole/Privilege/FunctionList.aspx?ParentId=' + id + '&ParentName=' + encodeURI(record.text);
}

function loadOperatorContent(id, record) {
    document.getElementById("content_frame").src = Global.contextPath + '/Modules/SecurityConsole/Privilege/OperationList.aspx?ParentId=' + id + '&ParentName=' + encodeURI(record.text);
}

function loadOperatorRuleContent(id, record) {
    document.getElementById("content_frame").src = Global.contextPath + '/Modules/SecurityConsole/Privilege/OperationRuleList.aspx?Operation=' + id + '&OperationName=' + encodeURI(record.text) + '&ElementId=' + (record.elementId);
}

/*************************************************
*****************模块操作*************************
*************************************************/
function addModule() {
    var parentId = this.data.parent_id;
    if (parentId == 'root') {
        parentId = '';
    }
    var parentName = this.data.parent_name;
    var url = path + '/Modules/SecurityConsole/Privilege/ModuleEdit.aspx?FormViewMode=Insert&ParentId=' + parentId + '&ParentName=' + parentName +'&title=' + encodeURI('模块新增');
    var result = showCenterModalDialog(url, 500, 140);
    reloadTree();
}

function updateModule() {
    var id = this.data.id;
    var parentId = this.data.parent_id;
    var parentName = this.data.parent_name;
    if (parentId == null || parentId == "null") {
        parentId = "";
        parentName = encodeURI("系统功能");
    }
    
    var url = path + '/Modules/SecurityConsole/Privilege/ModuleEdit.aspx?FormViewMode=Edit&Id=' + id + '&ParentId=' + parentId + '&ParentName=' + parentName + '&title=' + encodeURI('模块修改');
    var result = showCenterModalDialog(url, 500, 140);
    reloadTree();
}

function deleteModule() {
    var id = this.data.id;
 
    if (window.confirm("确认要删除该模块吗？")) {
        $.dataservice("PrivilegeService$DeleteModule", { moduleId: id }, function (response) {
                    reloadTree();
        });
    }
}

/*************************************************
*****************页面操作*************************
*************************************************/
/*新建功能页面*/
function addFunction() {
    var parentId = this.data.parent_id;
    var parentName = this.data.parent_name;
    var url = path + '/Modules/SecurityConsole/Privilege/FunctionEdit.aspx?FormViewMode=Insert&ParentId=' + parentId + '&ParentName=' + parentName + '&title=' + encodeURI('功能新增');
    if (showCenterModalDialog(url, 550, 330)) {
        reloadTree();
    }
}


/*修改功能页面*/
function updateFunction() {
    var id = this.data.id;
    var parentId = this.data.parent_id;
    var parentName = this.data.parent_name;
    var url = path + '/Modules/SecurityConsole/Privilege/FunctionEdit.aspx?FormViewMode=Edit&Id=' + id + '&ParentId=' + parentId + '&ParentName=' + parentName + '&title=' + encodeURI('功能修改');
    if (showCenterModalDialog(url, 550, 330)) {
        reloadTree();
    }
}

/*删除*/
function deleteFunction() {
    var functionId = this.data.id;

    if (window.confirm("确认要删除该功能吗？")) {
        $.dataservice("PrivilegeService$DeleteFunction", { functionId: functionId }, function (response) {
            alert("删除功能成功！");
            reloadTree();
        });
    }
}

/*************************************************
*****************页面元素操作*************************
*************************************************/
function addOperation() {
    var parentId = this.data.parent_id;
    var parentName = this.data.parent_name;
    var url = path + '/Modules/SecurityConsole/Privilege/OperationEdit.aspx?FormViewMode=Insert&ParentId=' + parentId + '&ParentName=' + parentName + '&title=' + encodeURI('操作新增');
    var result = showCenterModalDialog(url, 600, 400);
    reloadTree();
}

function updateOperation() {
    var id = this.data.id;
    var parentId = this.data.parent_id;
    var parentName = this.data.parent_name;
    var url = path + '/Modules/SecurityConsole/Privilege/OperationEdit.aspx?FormViewMode=Edit&Id=' + id + '&Parent=' + parentId + '&ParentName=' + parentName + '&title=' + encodeURI('操作修改');
    var result = showCenterModalDialog(url, 600, 400);
    reloadTree();
}

function deleteOperation() {
    var operationId = this.data.id;

    if (window.confirm("确认要删除该操作吗？")) {
        $.dataservice("PrivilegeService$DeleteOperation", { operationId: operationId }, function (response) {
            alert("删除操作成功！");
            reloadTree();
        });
    }
}

function reloadTree() {
    treeInstance.reload();
    treeInstance.expandLevel(null, 3);
    document.getElementById("content_frame").src = document.getElementById("content_frame").src;
}
